package org.abacus.dbtojava;

import java.util.Random;
import java.util.StringTokenizer;

public class ContentBuilderTools {
	private static final ContentBuilderTools instance = new ContentBuilderTools();
	private Random RANDOM = new Random();
	private Object default_field_access_modifier_config = Config.getString("default_field_access_modifier");
	private String default_field_access_modifier = "private";

	private Object default_method_access_modifier_config = Config.getString("default_method_access_modifier");
	private String default_method_access_modifier = "public";

	private ContentBuilderTools() {
		super();
	}

	public static ContentBuilderTools getInstance() {
		return instance;
	}

	/**
	 *
	 * @param s
	 * @return
	 */
	public String firstToUpperCase(String s) {
		if (null == s) {
			return null;
		}
		if ("".equals(s)) {
			return s;
		}
		return s.substring(0, 1).toUpperCase().concat(s.substring(1));
	}

	/**
	 *
	 * @param s
	 * @return
	 */
	public String firstToLowerCase(String s) {
		if (null == s) {
			return null;
		}
		if ("".equals(s)) {
			return s;
		}
		return s.substring(0, 1).toLowerCase().concat(s.substring(1));
	}

	/**
	 *
	 * @param s
	 * @return
	 */
	public String getClassName(String s) {
		if (null == s) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		StringTokenizer st = new StringTokenizer(s.toLowerCase().replace("-", ""), "_");
		String next;
		while (st.hasMoreElements()) {
			next = (String) st.nextElement();
			sb.append(firstToUpperCase(next));
		}
		return sb.toString();
	}

	/**
	 *
	 * @param s
	 * @return
	 */
	public String getAttributeName(String s) {
		if (null == s) {
			return null;
		}
		return this.firstToLowerCase(this.getClassName(s));
	}

	public String getSerialVersionUID() {
		StringBuffer sb = new StringBuffer();
		sb.append(Config.TAB);
		sb.append("private static final long serialVersionUID = " + RANDOM.nextLong() + "L");
		sb.append(Config.SEMICOLON);
		sb.append(Config.NEW_LINE);
		sb.append(Config.NEW_LINE);
		return sb.toString();
	}

	/**
	 *
	 * @param columnType
	 * @param attributeName
	 * @return
	 */
	public String getAttributeContent(String columnType, String attributeName) {
		StringBuffer sb = new StringBuffer();
		sb.append(Config.TAB);
		sb.append(null == default_field_access_modifier_config ? default_field_access_modifier : default_field_access_modifier_config);
		sb.append(Config.SPACE);
		sb.append(columnType);
		sb.append(Config.SPACE);
		sb.append(attributeName);
		sb.append(Config.SEMICOLON);
		sb.append(Config.NEW_LINE);
		sb.append(Config.NEW_LINE);
		return sb.toString();
	}

	/**
	 *
	 * @param columnType
	 * @param attributeName
	 * @return
	 */
	public String getGetterContent(String columnType, String attributeName) {
		StringBuffer sb = new StringBuffer();
		sb.append(Config.TAB);
		sb.append(null == default_method_access_modifier_config ? default_method_access_modifier : default_method_access_modifier_config);
		sb.append(Config.SPACE);
		sb.append(columnType);
		sb.append(Config.SPACE);
		sb.append(Config.GETTER);
		sb.append(this.firstToUpperCase(attributeName));
		sb.append("() {");
		sb.append(Config.NEW_LINE);
		sb.append(Config.TAB);
		sb.append(Config.TAB);
		sb.append(Config.RETURNS);
		sb.append(Config.SPACE);
		sb.append(attributeName);
		sb.append(Config.SEMICOLON);
		sb.append(Config.NEW_LINE);
		sb.append(Config.TAB);
		sb.append("}");
		sb.append(Config.NEW_LINE);
		sb.append(Config.NEW_LINE);
		return sb.toString();
	}

	/**
	 *
	 * @param columnType
	 * @param attributeName
	 * @return
	 */
	public String getSetterContent(String columnType, String attributeName) {
		StringBuffer sb = new StringBuffer();
		sb.append(Config.TAB);
		sb.append(null == default_method_access_modifier_config ? default_method_access_modifier : default_method_access_modifier_config);
		sb.append(Config.SPACE);
		sb.append("void");
		sb.append(Config.SPACE);
		sb.append(Config.SETTER);
		sb.append(this.firstToUpperCase(attributeName));
		sb.append("(");
		sb.append(columnType);
		sb.append(Config.SPACE);
		sb.append(attributeName);
		sb.append(") {");
		sb.append(Config.NEW_LINE);
		sb.append(Config.TAB);
		sb.append(Config.TAB);
		sb.append("this.");
		sb.append(attributeName);
		sb.append(" = ");
		sb.append(attributeName);
		sb.append(Config.SEMICOLON);
		sb.append(Config.NEW_LINE);
		sb.append(Config.TAB);
		sb.append("}");
		sb.append(Config.NEW_LINE);
		sb.append(Config.NEW_LINE);
		return sb.toString();
	}

}
